#include "stdafx.h"
#include "ShellContextMenu.h"

//--------------------------------------------------------------------------------------
HWND CreateMyWindow()
{
	TCHAR szWindowClass[] = TEXT("expopup_windowclass");
	
	WNDCLASSEX wcex;
	wcex.cbSize = sizeof(WNDCLASSEX);
	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= DefWindowProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= NULL;
	wcex.hIcon			= NULL;
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= NULL;
	wcex.lpszClassName	= szWindowClass;
	wcex.hIconSm		= NULL;

	RegisterClassEx(&wcex);

	return CreateWindow(szWindowClass, NULL, WS_OVERLAPPEDWINDOW,
						CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, NULL, NULL);
}

//--------------------------------------------------------------------------------------
extern "C"
__declspec( dllexport )BOOL ShowContextMenu(char* path, int x, int y)
{
	try
	{
		if (!AfxOleInit())
		{
			AfxMessageBox(TEXT ("AfxOleInit Error!"));
			return FALSE;
		}
		HWND hWnd = CreateMyWindow();
		SetCursor(LoadCursor(NULL, IDC_ARROW));
		CStringArray arrayFiles;
		arrayFiles.Add (path);
		CShellContextMenu scm;
		scm.SetObjects (arrayFiles);
		CPoint point (x, y);
		UINT idCommand = scm.ShowContextMenu (hWnd, point);
		DestroyWindow(hWnd);
		return TRUE;
	}
	catch (...)
	{
		return FALSE;
	}
}

//--------------------------------------------------------------------------------------
int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
	ShowContextMenu("d:\\", 100, 100);
}
